package com.example.businesscalcproject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Amortizacao_Constante extends Activity {
	
	public String tabela = "";
	
	public double valor_financeiro = 0;
	public double prazo_meses = 0;
	public double juro_mensal = 0;
	
	public double AC = 0;
	public double acumulada = 0;
	public double res = 0;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// TODO Put your code here
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.amortizacao_constante);
		Log.d("Call","Entro");
		
		Button tabela = (Button) findViewById(R.id.cal_amortizacao);
		tabela.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				EditText Valor = (EditText) findViewById(R.id.vl_fin);
				valor_financeiro = Double.parseDouble(Valor.getText().toString());

				EditText residual = (EditText) findViewById(R.id.vl_prazo_meses);
				prazo_meses = Double.parseDouble(residual.getText()
						.toString());

				EditText vida = (EditText) findViewById(R.id.taxa_juros_mensal);
				juro_mensal = Double.parseDouble(vida.getText().toString());
				conta();
				enviatable();

			}
		});
		
	}
	
	public void enviatable() {
		Intent intent = new Intent(this, Tabela_Amortizacao_Constante.class);
		intent.putExtra("html", tabela);
		startActivity(intent);
	}
	
	public void conta() {
		for (int x = 0; x <= juro_mensal; x++) {
			if (x == 0) {
				tabela += "<tr>" + "<td width='75' nowrap='nowrap'>"
						+ "<p style='text-align: center'>0</p>" + "</td>"
						+ "<td width='164' nowrap='nowrap'>"
						+ "<p style='text-align: center'>-x-</p>" + "</td>"
						+ "<td width='180' nowrap='nowrap'>"
						+ "<p style='text-align: center'>-x-</p>" + "</td>"
						+ "<td width='153' nowrap='nowrap'>"
						+ "<p style='text-align: center'>" + valor_financeiro + "</p>"
						+ "</td>" + "</tr>";
			}

			else {
				AC = (valor_financeiro - prazo_meses) / juro_mensal;
				acumulada = acumulada + AC;
				res = valor_financeiro - acumulada;
				
				tabela += "<tr>" + "<td width='75' nowrap='nowrap'>"
						+ "<p style='text-align: center'>" + x + "</p>"
						+ "</td>" + "<td width='164' nowrap='nowrap'>"
						+ "<p style='text-align: center'>" + AC + "</p>"
						+ "</td>" + "<td width='180' nowrap='nowrap'>"
						+ "<p style='text-align: center'>" + acumulada + "</p>"
						+ "</td>" + "<td width='153' nowrap='nowrap'>"
						+ "<p style='text-align: center'>" + res + "</p>"
						+ "</td>" + "</tr>";
			}

		}
	}

}
